clear all
set more off

do "set_path.do"

***** I. PRODUCE CHARTS *****

use "${datadir}cip_all_both.dta", clear

gen cip_rf=(r-r_usd-rho)*100

rangestat cip_govt_mean = cip_govt, interval(date -13 0) by(currency tenor)
rangestat cip_rf_mean = cip_rf, interval(date -13 0) by(currency tenor)
keep group currency tenor date cip_govt_mean cip_rf_mean

drop if date < mdy(1,1,2000)

* Fill-in data for missing date so we don't have a median with too few observations
reshape wide cip_govt_mean cip_rf_mean, i(group tenor date) j(currency) string
reshape long cip_govt_mean cip_rf_mean, i(group tenor date) j(currency) string
sort group currency tenor date
bys group currency tenor: replace cip_govt_mean = cip_govt_mean[_n-1] if missing(cip_govt_mean) & !missing(cip_govt_mean[_n-1])
bys group currency tenor: replace cip_rf_mean = cip_rf_mean[_n-1] if missing(cip_rf_mean) & !missing(cip_rf_mean[_n-1])

collapse (mean) cip_govt_mean cip_rf_mean, by(group tenor date)

* Change sign of USD Convenience 
twoway (tsline cip_govt_mean if group=="g10" & tenor=="3m", cmissing(no) lwidth(medthin) lcolor(red) lpattern(solid)  legend(label(1 "Government CIP Deviation"))) ///
	   (tsline cip_rf_mean if group=="g10" & tenor=="3m", cmissing(no) lwidth(medthin) lcolor(blue) lpattern(solid) legend(label(2 "Benchmark CIP Deviation"))), ///
		yline(0, lwidth(vthin) lcolor(black) lstyle(solid)) ylabel(-50(50)300,labsize(small) nogrid) xlabel(#12, labsize(small) format(%tdCY) nogrid) ///
		xtitle("") ytitle("Basis Points", size(small)) legend(size(small)) ///
		graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) ///
		subtitle(, size(small) ring(0) pos(1) nobexpand) note("") ///
		legend(region(lstyle(none)) rows(1) position(bottom))
		// title("3M", size(medium))
graph export "${outdir}figures/cip_xccy_3m.pdf", replace

twoway (tsline cip_govt_mean  if group=="g10" & tenor=="10y", cmissing(no) lwidth(medthin) lcolor(red) lpattern(solid)  legend(label(1 "Government CIP Deviation"))) ///
	   (tsline cip_rf_mean if group=="g10" & tenor=="10y", cmissing(no) lwidth(medthin) lcolor(blue) lpattern(solid) legend(label(2 "Benchmark CIP Deviation"))), ///	   
		yline(0, lwidth(vthin) lcolor(black) lstyle(solid)) ylabel(-50(25)75,labsize(small) nogrid) xlabel(#12, labsize(small) format(%tdCY) nogrid) ///
		xtitle("") ytitle("Basis Points", size(small)) legend(size(small)) ///
		graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) ///
		subtitle(, size(small) ring(0) pos(1) nobexpand) note("") ///
		legend(region(lstyle(none)) rows(1) position(bottom))
		// title("10Y", size(medium)) 
graph export "${outdir}figures/cip_xccy_10y.pdf", replace

